package com.zy.openchat.core.model.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;
import com.zy.openchat.App;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgEvent;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;

public class TabExMenuInfo implements Parcelable {
    //菜单标题
    public String title;
    //菜单URL
    public String url;

    public String icon;

    public String status;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.title);
        dest.writeString(this.url);
        dest.writeString(this.icon);
        dest.writeString(this.status);
    }

    public TabExMenuInfo() {
    }

    protected TabExMenuInfo(Parcel in) {
        this.title = in.readString();
        this.url = in.readString();
        this.icon = in.readString();
        this.status = in.readString();
    }

    public static final Creator<TabExMenuInfo> CREATOR = new Creator<TabExMenuInfo>() {
        @Override
        public TabExMenuInfo createFromParcel(Parcel source) {
            return new TabExMenuInfo(source);
        }

        @Override
        public TabExMenuInfo[] newArray(int size) {
            return new TabExMenuInfo[size];
        }
    };

    public boolean isValid() {
        return !StringUtils.isEmpty(title) && !StringUtils.isEmpty(url) && !StringUtils.isEmpty(icon);
    }

    //本地缓存
    public static TabExMenuInfo getTabExMenuInfo() {
        Context context = App.getInstanse();
        String jsonStr = context.getSharedPreferences("config", Context.MODE_PRIVATE).getString("tabExMenu", null);
        if (jsonStr != null)
            return new Gson().fromJson(jsonStr, TabExMenuInfo.class);
        return null;
    }

    public static void saveTabExMenuInfo(TabExMenuInfo info) {
        if (info == null) {
            return;
        }
        TabExMenuInfo curMenuInfo = getTabExMenuInfo();
        //保存数据
        String jsonStr = new Gson().toJson(info);
        Context context = App.getInstanse();
        SharedPreferences.Editor editor = context.getSharedPreferences("config", Context.MODE_PRIVATE).edit();
        editor.putString("tabExMenu", jsonStr);
        editor.apply();
        //比较
        if (curMenuInfo == null) {
            if (info.isValid()) {
                //tab菜单变化
                EventBus.getDefault().post(TgEvent.event(TelegramManager.EUser_TgEvent.EUser_Tab_Ex_Menu_Changed, null));
            }
        } else {
            if (info.isValid() != curMenuInfo.isValid()) {
                //tab菜单变化
                EventBus.getDefault().post(TgEvent.event(TelegramManager.EUser_TgEvent.EUser_Tab_Ex_Menu_Changed, null));
            }
            if (info.isValid() && curMenuInfo.isValid()) {
                if (!info.title.equals(curMenuInfo.title) || !info.url.equals(curMenuInfo.url)|| !info.icon.equals(curMenuInfo.icon)
                        || !info.status.equals(curMenuInfo.status)
                ) {
                    //tab菜单变化
                    EventBus.getDefault().post(TgEvent.event(TelegramManager.EUser_TgEvent.EUser_Tab_Ex_Menu_Changed, null));
                }
            }
        }
    }
}
